package com.ruoyi.epidemic.service.impl;

import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.epidemic.domain.TbHistory;
import com.ruoyi.epidemic.domain.TbProvince;
import com.ruoyi.epidemic.service.ITbProvinceService;
import com.ruoyi.epidemic.vo.CountVo;
import com.ruoyi.epidemic.vo.DeadVo;
import com.ruoyi.epidemic.vo.DetailsVo;
import com.ruoyi.epidemic.vo.MessageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.ruoyi.epidemic.mapper.TbDetailsMapper;
import com.ruoyi.epidemic.domain.TbDetails;
import com.ruoyi.epidemic.service.ITbDetailsService;

import javax.annotation.Resource;

/**
 * 省市确诊数据Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-03-21
 */
@Service
public class TbDetailsServiceImpl implements ITbDetailsService 
{
    @Resource
    private TbDetailsMapper tbDetailsMapper;
    @Resource
    private ITbProvinceService provinceService;

    @Value("${ruoyi.criticalValue}")
    private Integer criticalValue;
    /**
     * 查询省市确诊数据
     * 
     * @param id 省市确诊数据主键
     * @return 省市确诊数据
     */
    @Override
    public TbDetails selectTbDetailsById(Long id)
    {
        return tbDetailsMapper.selectTbDetailsById(id);
    }

    /**
     * 查询省市确诊数据列表
     * 
     * @param tbDetails 省市确诊数据
     * @return 省市确诊数据
     */
    @Override
    public List<TbDetails> selectTbDetailsList(TbDetails tbDetails)
    {
        return tbDetailsMapper.selectTbDetailsList(tbDetails);
    }

    /**
     * 新增省市确诊数据
     * 
     * @param tbDetails 省市确诊数据
     * @return 结果
     */
    @Override
    public int insertTbDetails(TbDetails tbDetails)
    {
        String province = tbDetails.getProvince();
        TbProvince tbProvince = provinceService.selectTbProvinceById(Long.parseLong(province));
        if (tbProvince!=null){
            tbDetails.setProvince(tbProvince.getProvince());
        }else {
            return -1;
        }
        tbDetails.setUpdateTime(new Date());
        return tbDetailsMapper.insertTbDetails(tbDetails);
    }

    /**
     * 修改省市确诊数据
     * 
     * @param tbDetails 省市确诊数据
     * @return 结果
     */
    @Override
    public int updateTbDetails(TbDetails tbDetails)
    {
        tbDetails.setUpdateTime(new Date());
        return tbDetailsMapper.updateTbDetails(tbDetails);
    }

    /**
     * 批量删除省市确诊数据
     * 
     * @param ids 需要删除的省市确诊数据主键
     * @return 结果
     */
    @Override
    public int deleteTbDetailsByIds(Long[] ids)
    {
        return tbDetailsMapper.deleteTbDetailsByIds(ids);
    }

    /**
     * 删除省市确诊数据信息
     * 
     * @param id 省市确诊数据主键
     * @return 结果
     */
    @Override
    public int deleteTbDetailsById(Long id)
    {
        return tbDetailsMapper.deleteTbDetailsById(id);
    }

    @Override
    public AjaxResult getList() {
        List<DetailsVo> list = tbDetailsMapper.getList();
        return AjaxResult.success(list);
    }

    @Override
    public AjaxResult count() {

        CountVo countVo =  tbDetailsMapper.getCount();
        return AjaxResult.success(countVo);
    }

    @Override
    public AjaxResult countProvince() {
        List<DetailsVo> list = tbDetailsMapper.countProvince();
        return AjaxResult.success(list);
    }

    @Override
    public TbHistory selectTbDetailsCount() {
        return tbDetailsMapper.selectTbDetailsCount();
    }

    @Override
    public void createMessage() {
        tbDetailsMapper.createMessage();
    }

    @Override
    public AjaxResult message() {
        List<MessageVo> list = new ArrayList<>();
        List<TbDetails> message = tbDetailsMapper.createMessage();
        if (CollectionUtils.isEmpty(message)) {
        return AjaxResult.success("近期没有数据");
        }
        for (int i = 0; i < message.size(); i++) {
            TbDetails tbDetails = message.get(i);
            if (tbDetails.getConfirm()>criticalValue){
                MessageVo messageVo = new MessageVo();
                messageVo.setMessage("最新数据消息："+tbDetails.getProvince()+"省今日确诊人数达到"+tbDetails.getConfirm()+"人，已超过系统设置临界值:"+criticalValue+"，出行做好防疫措施。");
                list.add(messageVo);
            }
        }
        return AjaxResult.success(list);
    }

    @Override
    public AjaxResult countDead() {
        List<DeadVo> deadVos = tbDetailsMapper.countDead();
        return AjaxResult.success(deadVos);
    }

    @Override
    public int saveDetails(TbDetails tbDetails) {
        return tbDetailsMapper.insertTbDetails(tbDetails);
    }

    @Override
    public TbDetails selectByCity(String substring) {
        QueryWrapper<TbDetails> tbDetailsQueryWrapper = new QueryWrapper<>();
        tbDetailsQueryWrapper.eq("city",substring);
        return tbDetailsMapper.selectOne(tbDetailsQueryWrapper);
    }

}
